package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomerDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * ClassName: CustomerController
 * Package: com.qk.controller
 * Description:
 *
 * @Author zt
 * @Create 2025/8/5 20:33
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/customers")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /*
    1.客户列表查询
     */
    @GetMapping
    public Result customerList(CustomerDto customerDto) {
        log.info("客户列表查询{}",customerDto);
        //调用service，分页查询，自定义的分页结果类
        PageResult<Customer> pageResult = customerService.listCustomers(customerDto);
        return Result.success(pageResult);
    }

    /*
    2.客户添加
     */
    @PostMapping
    public Result add(@RequestBody Customer customer) {
        log.info("添加客户：{}", customer);
        //补全熟悉，创建时间，更新时间
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerService.save(customer);
        return Result.success();
    }

    /*
    3.根据ID查询（查询回显）
     */
    @GetMapping("/{id}")
    public Result getCustomerById(@PathVariable Integer id) {
        log.info("根据id查询用户:{}", id);
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }

    /*
    4.更新用户
     */
    @PutMapping
    public Result update(@RequestBody Customer customer) {
        log.info("更新用户: {}", customer);
        customerService.updateById(customer);
        return Result.success();
    }
}
